package org.restorator.service.impl;

import org.restorator.dao.base.OrderDao;
import org.restorator.dao.implement.OrderDaoImpl;
//import org.restorator.dao.implement.OrderDaoMock;
import org.restorator.dao.tx.TransactionManager;
import org.restorator.dao.tx.TransactionManagerJdbc;
import org.restorator.dao.tx.TxManagerJdbc;
import org.restorator.entity.Item;
import org.restorator.entity.Order;
import org.restorator.entity.dto.OrderDto;
import org.restorator.exception.ServiceException;
import org.restorator.service.OrderService;
import org.restorator.utill.GeneralUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.Callable;

/**
 * @author Snisar Bogdan.
 */
public class OrderServiceImpl implements OrderService {

    private OrderDao orderDao = new OrderDaoImpl();
    private TransactionManager tx = TxManagerJdbc.INSTANCE;
    private static Logger logger = LoggerFactory.getLogger(GeneralUtil.getClassName());


    @Override
    public Order getOrderByTableId(final int id) throws ServiceException {
        return tx.doInTran(new Callable<Order>() {
            @Override
            public Order call() throws Exception {


                return orderDao.getOrdersByTableId(id);
            }
        });
    }


    @Override
    public Order get(final Integer primaryKey) throws ServiceException {
        return tx.doInTran(new Callable<Order>() {
            @Override
            public Order call() throws Exception {
                return orderDao.get(primaryKey);
            }
        });
    }

    @Override
    public void update(final Order entity) throws ServiceException {
        logger.debug("Delete ORDER {}", entity);
        tx.doInTran(new Callable<Order>() {
            @Override
            public Order call() throws Exception {
                return orderDao.update(entity);
            }
        });
    }

    @Override
    public Integer save(final Order entity, final Object... arg) throws ServiceException {
        Order order = entity;
        Integer id =  tx.doInTran(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                return orderDao.save(entity, arg);
            }

        });
        order.setOrderId(id);
        return id;
    }


    @Override
    public void delete(final Order entity) throws ServiceException {
        tx.doInTran(new Callable<Order>() {
            @Override
            public Order call() throws Exception {
                orderDao.delete(entity);
                return null;
            }
        });
    }

    @Override
    public List<Order> getAll() throws ServiceException {
        return tx.doInTran(new Callable<List<Order>>() {
            @Override
            public List<Order> call() throws Exception {
                return orderDao.getAll();
            }
        });
    }

    @Override
    public OrderDto toDto(Order actual) {
        OrderDto d = null;
        if(actual != null) {
            d = new OrderDto();
            d.setOrderId(actual.getOrderId());
            ArrayList<Integer> list = new ArrayList<>(actual.getItems().size());
            for(Item i : actual.getItems()) {
                list.add(i.getItemId());
            }
            d.setItemsId(list);
        }
        return d;
    }

    @Override
    public Collection<OrderDto> toDto(Collection<Order> actual) {
        ArrayList<OrderDto> l = null;
        if(actual != null) {
            l = new ArrayList<>(actual.size());
            for(Order o : actual) {
                l.add(toDto(o));
            }
        }
        return l;
    }

    @Override
    public Order fromDto(OrderDto dto) {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public Collection<Order> fromDto(Collection<OrderDto> dtos) {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }
}
